// 订单反馈
import { client } from '../../request/index.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    imgIp: getApp().globalData.imgIp,
    titleFontNum: 0,
    imgbox: [],
    uri: [],
  },
  // 输入框字数统计
  countNum: function (e) {
    this.setData({
      titleFontNum: e.detail.value.length,
    })
  },
  // 添加图片
  chooseImage: function () {
    var that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: '#000000',
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.addImg('album')
          } else if (res.tapIndex == 1) {
            that.addImg('camera')
          }
        }
      },
    })
  },
  addImg: function (type) {
    var imgbox = this.data.imgbox
    var that = this
    wx.chooseImage({
      count: 3, // 最多3张
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: [type], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        imgbox = imgbox.concat(tempFilePaths)
        that.setData({
          imgbox: imgbox,
        })
      },
    })
  },
  //删除图片
  imgDelete: function (e) {
    console.log(e)
    let that = this
    let index = e.currentTarget.dataset.deindex
    let imgbox = this.data.imgbox
    imgbox.splice(index, 1)
    that.setData({
      imgbox: imgbox,
    })
  },
  formSubmit: function (e, index = 0) {
    var that = this
    if (!that.check(e))
      //表单验证
      return
    if (index == that.data.imgbox.length) return
    console.log('正在上传：' + that.data.imgbox[index])
    wx.uploadFile({
      //上传包含图片的form表单
      url: getApp().globalData.ip + '/upload',
      filePath: that.data.imgbox[index], // 图片文件路径
      name: 'file',
      header: {
        cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
        'content-type': 'charset=utf-8',
      },
      dataType: 'json',
      success: function (res) {
        let datas = JSON.parse(res.data)
        console.log('imgbox[' + index + ']上传成功！')
        var new_url = that.data.uri.concat(datas.data)
        console.log(new_url)

        that.setData({
          uri: new_url,
        })
        if (index == that.data.imgbox.length - 1) {
          that.uploadData(e.detail.value)
        } else that.formSubmit(e, index + 1)
      },
    })
    that.setData({
      titleFontNum: 0,
    })
  },

  async uploadData(v) {
    let that = this
    const sessId = getApp().globalData.sessionid
    const getData = await client({
      url: '/feedback/order_fbmessage',
      method: 'POST',
      data: {
        content: v.content,
        cover_pic_url: that.data.uri.join(''),
      },
    })
    console.log('订单反馈', getData)
    that.onLoad()
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000,
    })
  },
  check: function (e) {
    //表单验证
    if (e.detail.value.content == '') {
      Dialog.alert({
        title: '提示',
        message: '请输入内容描述',
        confirmButtonText: '返回输入',
      })
      return false
    }
    if (this.data.imgbox.length == 0) {
      Dialog.alert({
        title: '提示',
        message: '请上传照片',
        confirmButtonText: '返回上传',
      })
      return false
    }
    return true
  },

  onLoad: function (options) {
    this.setData({
      uri: [],
      imgbox: [],
      content: '',
    })
  },

  onPullDownRefresh: function () {},
})
